﻿using System;
using System.Collections.Generic;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x0200003A RID: 58
    public static class AliPayProxy {
        // Token: 0x060001A1 RID: 417 RVA: 0x000031E0 File Offset: 0x000013E0
        public static void AddAliPayProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("GET/gateway.do", new HttpContextActionDelegate(AliPay));
            svr.RouterList.Add("POST/gateway.do", new HttpContextActionDelegate(AliPay));
        }

        // Token: 0x060001A2 RID: 418 RVA: 0x0000CE6C File Offset: 0x0000B06C
        private static void AliPay(HttpContext ctx) {
            AliPayRequest aliPayRequest = (ctx.Request.Form.Count > 0) ? new AliPayRequest(ctx.Request.Form) : new AliPayRequest(ctx.Request.UrlForm);
            AliPayResult aliPayResult = new AliPayResult {
                code = "10000",
                trade_no = aliPayRequest.trade_no,
                out_trade_no = aliPayRequest.out_trade_no
            };
            try {
                HongshPayTrade hongshPayTrade;
                if (aliPayRequest.method.Equals("alipay.trade.pay", StringComparison.OrdinalIgnoreCase)) {
                    hongshPayTrade = ctx.Execute(new TradeAuthPaymentRequest {
                        ClientTradeId = aliPayRequest.out_trade_no,
                        Amount = (int)(aliPayRequest.total_amount * 100m),
                        Authcode = aliPayRequest.auth_code,
                        Subject = aliPayRequest.subject
                    });
                    if (hongshPayTrade.State == TradeState.失败 || hongshPayTrade.State == TradeState.关闭) {
                        throw new Exception("交易失败或超时，请撤销交易！");
                    }
                    if (hongshPayTrade.State != TradeState.成功) {
                        aliPayResult.code = "10003";
                    }
                }
                else {
                    if (!aliPayRequest.method.Equals("alipay.trade.query", StringComparison.OrdinalIgnoreCase)) {
                        ctx.Result = HttpResult.NoFound;
                        return;
                    }
                    hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                        TradeId = aliPayRequest.trade_no,
                        ClientTradeId = aliPayRequest.out_trade_no
                    });
                    if (hongshPayTrade.State == TradeState.成功) {
                        aliPayResult.trade_status = "TRADE_SUCCESS";
                    }
                    else if (hongshPayTrade.State == TradeState.失败 || hongshPayTrade.State == TradeState.关闭) {
                        aliPayResult.trade_status = "TRADE_CLOSED";
                    }
                    else {
                        aliPayResult.trade_status = "WAIT_BUYER_PAY";
                    }
                }
                aliPayResult.total_amount = hongshPayTrade.Amount / 100;
                aliPayResult.buyer_pay_amount = hongshPayTrade.ActualAmount / 100;
            }
            catch (Exception ex) {
                aliPayResult.code = "40004";
                aliPayResult.subcode = "SYSTEM_ERROR";
                aliPayResult.msg = aliPayResult.sub_msg = ex.Message;
            }
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            string key = aliPayRequest.method.Replace(".", "_") + "_response";
            dictionary[key] = aliPayResult;
            ctx.Result = new HttpJsonResult(dictionary);
        }

        // Token: 0x0200003B RID: 59
        private class AliPayRequest : Dictionary<string, string> {
            // Token: 0x060001A3 RID: 419 RVA: 0x0000321A File Offset: 0x0000141A
            public AliPayRequest(IDictionary<string, string> req) {
                //Class15.cwcZ3SWztHroi();
                //base..ctor(req);
            }

            // Token: 0x170000A7 RID: 167
            // (get) Token: 0x060001A4 RID: 420 RVA: 0x00003228 File Offset: 0x00001428
            public string method {
                get {
                    return this.ReadOrDefault("method");
                }
            }

            // Token: 0x170000A8 RID: 168
            // (get) Token: 0x060001A5 RID: 421 RVA: 0x0000D0D0 File Offset: 0x0000B2D0
            public Dictionary<string, object> biz_content {
                get {
                    if (_biz_content == null) {
                        _biz_content = this.ReadOrDefault("biz_content").JsonDecode<Dictionary<string, object>>();
                    }
                    return _biz_content;
                }
            }

            // Token: 0x170000A9 RID: 169
            // (get) Token: 0x060001A6 RID: 422 RVA: 0x00003235 File Offset: 0x00001435
            public string trade_no {
                get {
                    return (string)biz_content.ReadOrDefault("trade_no");
                }
            }

            // Token: 0x170000AA RID: 170
            // (get) Token: 0x060001A7 RID: 423 RVA: 0x00003247 File Offset: 0x00001447
            public string out_trade_no {
                get {
                    return (string)biz_content.ReadOrDefault("out_trade_no");
                }
            }

            // Token: 0x170000AB RID: 171
            // (get) Token: 0x060001A8 RID: 424 RVA: 0x00003259 File Offset: 0x00001459
            public string auth_code {
                get {
                    return (string)biz_content.ReadOrDefault("auth_code");
                }
            }

            // Token: 0x170000AC RID: 172
            // (get) Token: 0x060001A9 RID: 425 RVA: 0x0000326B File Offset: 0x0000146B
            public string subject {
                get {
                    return (string)biz_content.ReadOrDefault("subject");
                }
            }

            // Token: 0x170000AD RID: 173
            // (get) Token: 0x060001AA RID: 426 RVA: 0x0000327D File Offset: 0x0000147D
            public decimal total_amount {
                get {
                    return (decimal)biz_content.ReadOrDefault("total_amount");
                }
            }

            // Token: 0x040000B4 RID: 180
            private Dictionary<string, object> _biz_content;
        }

        // Token: 0x0200003C RID: 60
        private class AliPayResult {
            // Token: 0x170000AE RID: 174
            // (get) Token: 0x060001AB RID: 427 RVA: 0x0000328F File Offset: 0x0000148F
            // (set) Token: 0x060001AC RID: 428 RVA: 0x00003297 File Offset: 0x00001497
            public string code { get; set; }

            // Token: 0x170000AF RID: 175
            // (get) Token: 0x060001AD RID: 429 RVA: 0x000032A0 File Offset: 0x000014A0
            // (set) Token: 0x060001AE RID: 430 RVA: 0x000032A8 File Offset: 0x000014A8
            public string msg { get; set; }

            // Token: 0x170000B0 RID: 176
            // (get) Token: 0x060001AF RID: 431 RVA: 0x000032B1 File Offset: 0x000014B1
            // (set) Token: 0x060001B0 RID: 432 RVA: 0x000032B9 File Offset: 0x000014B9
            public string subcode { get; set; }

            // Token: 0x170000B1 RID: 177
            // (get) Token: 0x060001B1 RID: 433 RVA: 0x000032C2 File Offset: 0x000014C2
            // (set) Token: 0x060001B2 RID: 434 RVA: 0x000032CA File Offset: 0x000014CA
            public string sub_msg { get; set; }

            // Token: 0x170000B2 RID: 178
            // (get) Token: 0x060001B3 RID: 435 RVA: 0x000032D3 File Offset: 0x000014D3
            // (set) Token: 0x060001B4 RID: 436 RVA: 0x000032DB File Offset: 0x000014DB
            public string trade_no { get; set; }

            // Token: 0x170000B3 RID: 179
            // (get) Token: 0x060001B5 RID: 437 RVA: 0x000032E4 File Offset: 0x000014E4
            // (set) Token: 0x060001B6 RID: 438 RVA: 0x000032EC File Offset: 0x000014EC
            public string out_trade_no { get; set; }

            // Token: 0x170000B4 RID: 180
            // (get) Token: 0x060001B7 RID: 439 RVA: 0x000032F5 File Offset: 0x000014F5
            // (set) Token: 0x060001B8 RID: 440 RVA: 0x000032FD File Offset: 0x000014FD
            public string buyer_logon_id { get; set; }

            // Token: 0x170000B5 RID: 181
            // (get) Token: 0x060001B9 RID: 441 RVA: 0x00003306 File Offset: 0x00001506
            // (set) Token: 0x060001BA RID: 442 RVA: 0x0000330E File Offset: 0x0000150E
            public decimal total_amount { get; set; }

            // Token: 0x170000B6 RID: 182
            // (get) Token: 0x060001BB RID: 443 RVA: 0x00003317 File Offset: 0x00001517
            // (set) Token: 0x060001BC RID: 444 RVA: 0x0000331F File Offset: 0x0000151F
            public decimal buyer_pay_amount { get; set; }

            // Token: 0x170000B7 RID: 183
            // (get) Token: 0x060001BD RID: 445 RVA: 0x00003328 File Offset: 0x00001528
            public decimal receipt_amount {
                get {
                    return total_amount;
                }
            }

            // Token: 0x170000B8 RID: 184
            // (get) Token: 0x060001BE RID: 446 RVA: 0x00003328 File Offset: 0x00001528
            public decimal invoice_amount {
                get {
                    return total_amount;
                }
            }

            // Token: 0x170000B9 RID: 185
            // (get) Token: 0x060001BF RID: 447 RVA: 0x00003330 File Offset: 0x00001530
            // (set) Token: 0x060001C0 RID: 448 RVA: 0x00003338 File Offset: 0x00001538
            public string gmt_payment { get; set; }

            // Token: 0x170000BA RID: 186
            // (get) Token: 0x060001C1 RID: 449 RVA: 0x00003341 File Offset: 0x00001541
            // (set) Token: 0x060001C2 RID: 450 RVA: 0x00003349 File Offset: 0x00001549
            public string trade_status { get; set; }

            // Token: 0x170000BB RID: 187
            // (get) Token: 0x060001C3 RID: 451 RVA: 0x00003352 File Offset: 0x00001552
            public string buyerContractNo {
                get {
                    return buyer_logon_id;
                }
            }

            // Token: 0x170000BC RID: 188
            // (get) Token: 0x060001C4 RID: 452 RVA: 0x00003352 File Offset: 0x00001552
            public string buyer_user_id {
                get {
                    return buyer_logon_id;
                }
            }

            // Token: 0x060001C5 RID: 453 RVA: 0x0000286D File Offset: 0x00000A6D
            public AliPayResult() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }
    }
}
